package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.base.BaseController;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.SysLogSwitch;
import com.ccp.dev.system.service.SysLogSwitchService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 日志开关 信息操作处理控制器
 *
 * @author zw
 * @date 2019-07-18 19:04:01
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_SYSTEM + "/sysLogSwitch")
@Action(ownermodel= SysAuditModelType.SYSTEM_SETTING)
public class SysLogSwitchController extends BaseController {
    @Resource
    private SysLogSwitchService sysLogSwitchService;

    /**
     * 功能描述: 跳转到首页
     *
     * @return: java.lang.String
     * @auther: zw
     * @date: 2019-07-18 19:04:01
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description = "日志开关首页跳转页面",exectype = "页面跳转日志")
    public String list() {
        return "sysLogSwitch/list";
    }


    /**
     * 功能描述: 跳转到新增页面
     *
     * @return: java.lang.String
     * @auther: zw
     * @date: 2019-07-18 19:04:01
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + UrlConstants.MODULE_FUNCTION_ADD_UPDATE)
    @Action(description = "日志开关新增跳转页面",exectype = "页面跳转日志")
    public String add(HttpServletRequest request, ModelMap modelMap) {
        String id = RequestUtil.getString(request, "id");
        modelMap.put("id", id);
        return "sysLogSwitch/add";
    }

    /**
     * 功能描述:  数据保存通用方法
     *
     * @param: sysLogSwitch 保存的对象
     * @return: java.lang.Object
     * @auther: zw
     * @date: 2019-07-18 19:04:01
     */
    @RequestMapping("switchStatus")
    @ResponseBody
    @Action(description = "打开或关闭日志开关",
            detail = "<#if switchStatus == switchStatus_open>打开<#else>关闭</#if>【${switchName}】日志开关<#if success>成功<#else>失败</#if>",
            exectype = "编辑日志")
    public Object switchStatus(@RequestBody SysLogSwitch sysLogSwitch, HttpServletRequest request) {
        SysAuditThreadLocalHolder.putParamerter("switchStatus", sysLogSwitch.getStatus());
        SysAuditThreadLocalHolder.putParamerter("switchStatus_open", SysLogSwitch.STATUS_OPEN);
        SysAuditThreadLocalHolder.putParamerter("switchName", sysLogSwitch.getModel());
        try {
            sysLogSwitchService.switchStatus(sysLogSwitch);
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return success("数据更新成功");
        } catch (Exception e) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail("数据更新失败");
        }
    }

    /**
     * 功能描述:  数据保存通用方法
     *
     * @param: sysLogSwitch 保存的对象
     * @return: java.lang.Object
     * @auther: zw
     * @date: 2019-07-18 19:04:01
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_SAVE)
    @ResponseBody
    @Action(description = "保存日志开关数据",
            detail = "<#if add>添加<#else>更新</#if>【${switchName}】日志开关<#if success>成功<#else>失败</#if>",
            exectype = "编辑日志")
    public Object save(@RequestBody SysLogSwitch sysLogSwitch, HttpServletRequest request) {
        String id = sysLogSwitch.getId();
        SysAuditThreadLocalHolder.putParamerter("switchName", sysLogSwitch.getModel());
        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("add", true);
            //新增操作
            //新增数据前生成随机Id  和创建时间
            sysLogSwitch.setId(UUIDUtils.getUUIDFor32());
            sysLogSwitchService.fillData(sysLogSwitch,true);
            int result = sysLogSwitchService.add(sysLogSwitch);
            if (result > 0) {
                SysAuditThreadLocalHolder.putParamerter("success", true);
                return success("数据保存成功！");
            } else {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return fail("数据保存失败！");
            }
        } else {
            SysAuditThreadLocalHolder.putParamerter("add", false);
            //修改操作
            sysLogSwitchService.fillData(sysLogSwitch,false);
            int result = sysLogSwitchService.update(sysLogSwitch);
            if (result > 0) {
                SysAuditThreadLocalHolder.putParamerter("success", true);
                return success("数据更新成功");
            } else {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return fail("数据更新失败");
            }
        }

    }

    /**
     * 功能描述: 根据主键ID删除数据的通用删除方法
     *
     * @param: id 参数名称必须是"id"
     * @return: java.lang.Object
     * @auther: zw
     * @date: 2019-07-18 19:04:01
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    @Action(description = "根据主键ID删除日志开关数据",
            detail = "删除日志开关<#if showName>【${switchName}】</#if><#if success>成功<#else>失败</#if>",
            exectype = "删除日志")
    public Object delete(@RequestParam("id") String id) {
        SysAuditThreadLocalHolder.putParamerter("showName", false);
        if (StringUtil.isEmpty(id)) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail("删除失败，未传入数据唯一标识!");
        }
        SysLogSwitch sysLogSwitch = sysLogSwitchService.getOneById(id);
        if (StringUtil.isEmpty(sysLogSwitch)) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail("删除失败,数据已不存在!");
        }
        SysAuditThreadLocalHolder.putParamerter("showName", true);
        SysAuditThreadLocalHolder.putParamerter("switchName", sysLogSwitch.getModel());
        if (SysLogSwitch.FROM_TYPE_IN.equals(sysLogSwitch.getFromType())) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail("内部模块不可删除!");
        }
        int result = sysLogSwitchService.delById(id);
        if (result > 0) {
            SysAuditThreadLocalHolder.putParamerter("success", true);
            return success("删除成功!");
        } else {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail("删除失败!");
        }
    }

    /**
     * 功能描述: 根据主键id获取一条数据的通用查询方法
     *
     * @param: id 参数对象必须是"id"
     * @return: java.lang.Object
     * @auther: zw
     * @date: 2019-07-18 19:04:01
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_GET)
    @ResponseBody
    @Action(description = "根据主键id查询日志开关的一条数据",
            detail = "<#if success>获取【${switchName}】的详情成功<#else>未获取对应数据</#if>",
            exectype = "查询日志")
    public Object get(@RequestParam("id") String id) {
        SysLogSwitch sysLogSwitch = sysLogSwitchService.getOneById(id);

        if (null == sysLogSwitch) {
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return fail("未获取对应数据！");
        }
        SysAuditThreadLocalHolder.putParamerter("success", true);
        SysAuditThreadLocalHolder.putParamerter("switchName", sysLogSwitch.getModel());
        return success(sysLogSwitch, "获取数据成功！");
    }

    /**
     * 功能描述: 附带检索条件的分页查询
     *
     * @param: [request] http请求
     * @return: java.lang.Object
     * @auther: zw
     * @date: 2019-07-18 19:04:01
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description = "附带检索条件的分页查询日志开关",exectype = "查询日志")
    public Object list(HttpServletRequest request) {
        QueryFilter queryFilter = new QueryFilter(request, false);
        //分页数据
        List<SysLogSwitch> pageDatas = sysLogSwitchService.queryList(queryFilter);
        return success(pageDatas, pageDatas.size());
    }

}
